package vip.ifmm.lehouse.email.service.api.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import vip.ifmm.lehouse.common.dto.ServerResponse;
import vip.ifmm.lehouse.common.enums.status.EmailSenderStatus;
import vip.ifmm.lehouse.email.service.api.EmailSenderService;
import vip.ifmm.lehouse.email.service.helper.ThreadHelper;
import vip.ifmm.lehouse.email.service.util.EmailUtil;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * 邮件服务的接口实现
 *
 * @author Fish
 * <p>Email: fishinlove@163.com</p>
 * <p>created by 2019/06/17 21:46:17</p>
 */
@Slf4j
@Service
public class EmailSenderServiceImpl implements EmailSenderService {

    @Value("${spring.mail.username}")
    private String sender = "";

    // 发送邮件的工具类
    @Resource
    private JavaMailSender emailSender = null;

    // 线程帮助类
    @Resource
    private ThreadHelper threadHelper = null;

    @Override
    public ServerResponse<Object> sendTextEmail(String receiver, String topic, String content) {
        return sendTextEmail(new String[]{
                receiver
        }, topic, content);
    }

    @Override
    public ServerResponse<Object> sendHtmlEmail(String receiver, String topic, String content) {
        return sendHtmlEmail(new String[]{
                receiver
        }, topic, content);
    }

    @Override
    public ServerResponse<Object> sendTextEmail(String[] receivers, String topic, String content) {
        try {
            // 创建文本邮件
            SimpleMailMessage email = EmailUtil.createTextEmail(sender, topic, content, receivers);

            log.info("邮件发送！收件人：" + Arrays.toString(receivers) + "，主题：" + topic);
            emailSender.send(email); // 发送邮件
        } catch (Exception e) {
            log.error("邮件发送失败，收件人：" + Arrays.toString(receivers) + "，主题：" + topic, e);
            return ServerResponse.response(EmailSenderStatus.EMAIL_SENT_FAILED);
        }

        return ServerResponse.response(EmailSenderStatus.SUCCESS);
    }

    @Override
    public ServerResponse<Object> sendHtmlEmail(String[] receivers, String topic, String content) {
        try {
            // 创建复杂邮件，带有 MIME 类型的邮件
            MimeMessage message = emailSender.createMimeMessage();

            // 使用包装类进行包装初始化
            EmailUtil.mimeMessageWrapper(sender, topic, content, message, receivers);

            log.info("邮件发送！收件人：" + Arrays.toString(receivers) + "，主题：" + topic);
            emailSender.send(message); // 发送邮件
        } catch (Exception e) {
            log.error("邮件发送失败，收件人：" + Arrays.toString(receivers) + "，主题：" + topic, e);
            return ServerResponse.response(EmailSenderStatus.EMAIL_SENT_FAILED);
        }

        return ServerResponse.response(EmailSenderStatus.SUCCESS);
    }

    @Override
    public ServerResponse<Future<?>> sendTextEmailAsync(String receiver, String topic, String content) {
        return ServerResponse.response(
                EmailSenderStatus.WAITING_TO_SEND_EMAIL,

                // 提交邮件发送任务
                threadHelper.submit((Callable<ServerResponse>) () -> sendTextEmail(receiver, topic, content))
        );
    }

    @Override
    public ServerResponse<Future<?>> sendTextEmailAsync(String[] receivers, String topic, String content) {
        return ServerResponse.response(
                EmailSenderStatus.WAITING_TO_SEND_EMAIL,

                // 提交邮件发送任务
                threadHelper.submit((Callable<ServerResponse>) () -> sendTextEmail(receivers, topic, content))
        );
    }

    @Override
    public ServerResponse<Future<?>> sendHtmlEmailAsync(String receiver, String topic, String content) {
        return ServerResponse.response(
                EmailSenderStatus.WAITING_TO_SEND_EMAIL,

                // 提交邮件发送任务
                threadHelper.submit((Callable<ServerResponse>) () -> sendHtmlEmail(receiver, topic, content))
        );
    }

    @Override
    public ServerResponse<Future<?>> sendHtmlEmailAsync(String[] receivers, String topic, String content) {
        return ServerResponse.response(
                EmailSenderStatus.WAITING_TO_SEND_EMAIL,

                // 提交邮件发送任务
                threadHelper.submit((Callable<ServerResponse>) () -> sendHtmlEmail(receivers, topic, content))
        );
    }
}
